# @Time : 2020/6/25 15:04
# @Author : Fioman 
# @Phone : 13149920693
"""
接收函数作为参数,或者把函数作为结果返回的函数称为高阶函数.map就是一个高阶函数,
sorted函数也是.
"""
fruits = ['strawberry', 'fig', 'apple', 'cherry', 'raspberry', 'banana']

len_sorted = sorted(fruits, key=len)
print("len_sorted = {}".format(len_sorted))

str_sorted = sorted(fruits)
print("str_sorted = {}".format(str_sorted))

"""
map,filter,reduce的现代替代品
都使用列表推导式来代替了.
map和filter 和列表推导式的比较
"""


def fact(n):
    return 1 if n < 2 else n * fact(n - 1)


L = list(map(fact, range(6)))
print("L = {}".format(L))
L1 = [fact(n) for n in range(6)]  # 列表推导式也一样可以实现,并且列表推导式可读性更强.
print("L1 = {}".format(L1))

L2 = list(map(fact, filter(lambda n: n % 2, range(6))))
print("L2 = {}".format(L2))
L3 = [fact(n) for n in range(6) if n % 2]
print("L3 = {}".format(L3))